﻿Imports System.Data.SqlClient
Imports DevExpress.XtraPrinting

Public Class frmComportamientoCliente

    Private Sub btnBuscarCliente_Click(sender As Object, e As EventArgs) Handles btnBuscarCliente.Click
        ObtenerTodaInfo()
    End Sub

    Private Sub ObtenerTodaInfo()
        If Me.txtCodigoCliente.EditValue <> Nothing Then
            Try
                BuscarCliente()
                Me.SP_CompartamientoClienteCreditosTableAdapter.Fill(Me.MEGASTOREDataSet.SP_CompartamientoClienteCreditos, Me.txtCodigoCliente.EditValue)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Me.txtCodigoCliente.BackColor = Color.LemonChiffon
            MessageBox.Show("Ingrese el código de cliente.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.txtCodigoCliente.Focus()
        End If
    End Sub

    Private Sub RevertirPago()
        Dim resp As String = MessageBox.Show("¿Está seguro de revertir el pago seleccionado?", varTitulo, MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

        If resp = vbYes Then
            frmRevertirPago.Usuario = frmPrincipal.UsuarioLogin
            frmRevertirPago.CodCliente = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colCodCliente)
            frmRevertirPago.Cliente = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNombreCliente)
            frmRevertirPago.Monto = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colCuotaTotal)
            frmRevertirPago.NumFactura = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNumFactura)
            frmRevertirPago.NumCuota = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colNumCuota)
            frmRevertirPago.IdDetallePago = Me.gvCreditos.GetRowCellValue(Me.gvCreditos.FocusedRowHandle, colIdDetalle)
            frmRevertirPago.ShowDialog()
        End If
    End Sub

    Private Sub txtCodigoCliente_EditValueChanged(sender As Object, e As EventArgs) Handles txtCodigoCliente.EditValueChanged
        Me.txtCodigoCliente.BackColor = Color.White
    End Sub

    Private Sub BuscarCliente()
        If varConexionMegaStore.State = ConnectionState.Open Then
            varConexionMegaStore.Close()
        End If

        varConexionMegaStore.Open()
        Dim sql As String = String.Format("SELECT NombreCliente FROM Clientes WHERE CodCliente = '{0}'", Me.txtCodigoCliente.EditValue)
        Dim cmd As New SqlCommand(sql, varConexionMegaStore)
        Dim reader As SqlDataReader = cmd.ExecuteReader

        If reader.Read Then
            Me.txtNombreCliente.EditValue = reader.Item("NombreCliente").ToString
        Else
            MessageBox.Show("No se ha encontrado este cliente.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.txtNombreCliente.EditValue = Nothing
        End If

        varConexionMegaStore.Close()
    End Sub

    Private Sub txtCodigoCliente_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCodigoCliente.KeyDown
        If e.KeyCode = Keys.Enter Then
            ObtenerTodaInfo()
        End If
    End Sub

    Private Sub btnExportarWord_Click(sender As Object, e As EventArgs) Handles btnExportarWord.Click
        If sfdWord.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToRtf(sfdWord.FileName)
        End If
    End Sub

    Private Sub bnExportaXLS_Click(sender As Object, e As EventArgs) Handles bnExportaXLS.Click
        If sfdExcel.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToXls(sfdExcel.FileName)
        End If
    End Sub

    Private Sub btnExportaPDF_Click(sender As Object, e As EventArgs) Handles btnExportaPDF.Click
        If sfdPDF.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Dim ps As New PrintingSystem()
            Dim link As New PrintableComponentLink(ps)
            link.Component = gcCreditos
            link.Landscape = False
            link.CreateDocument()
            link.PrintingSystemBase.ExportToPdf(sfdPDF.FileName)
        End If
    End Sub

    Private Sub RevertirPagoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RevertirPagoToolStripMenuItem.Click
        RevertirPago()
    End Sub
End Class